package com.course.controller;

import com.course.convert.StrToListFormatter;
import com.course.entity.dto.CourseDto;
import com.course.service.ISubCampusCourseService;
import com.easyCode.feature.mybaits.CustomPage;
import com.easyCode.feature.result.ResponseResultAdvice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.DataBinder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

@Validated
@RestController
@RequestMapping("/subCampusCourse")
@RequiredArgsConstructor
@ResponseResultAdvice
@Api(tags = "分校区课程请求")
public class SubCampusCourseController {
    private final ISubCampusCourseService iSubCampusCourseService;

    @InitBinder
    public void initBinder(DataBinder dataBinder) {
        dataBinder.addCustomFormatter(new StrToListFormatter());
    }

    @PostMapping("/realse/{cId}")
    @ApiOperation("分配课程给某个分校区(普通管理员和超级管理员都可以)")
    public void releaseCoursesToSubCampus(@Positive(message = "非法参数") @PathVariable("cId") Integer cId,
                                       @ApiParam("课程id列表,由'-'分割课程id,如: 1-2-3") @RequestParam("courseIdList") List<Integer> courseIdList) {
        iSubCampusCourseService.releaseCoursesToCampus(cId, courseIdList);
    }

    @PostMapping("/revoke/{cId}")
    @ApiOperation("回收分配给分校区的课程(普通管理员和超级管理员都可以)")
    public void revokeCoursesFromSubCampus(@Positive(message = "非法参数") @PathVariable("cId") Integer cId,
                                        @ApiParam("课程id列表,由'-'分割课程id,如: 1-2-3") @RequestParam("courseIdList") List<Integer> courseIdList) {
        iSubCampusCourseService.revokeCoursesFromCampus(cId, courseIdList);
    }

    @PostMapping("/list/{cId}")
    @ApiOperation("查询分配给分校区的课程(普通管理员和超级管理员都可以)")
    public List<CourseDto> listCoursesReleaseToSubCampus(@Positive(message = "非法参数") @PathVariable("cId") Integer cId, CustomPage customPage) {
        return iSubCampusCourseService.listCoursesReleaseToCampus(cId, customPage);
    }
}
